package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

public class DoctorInfoDetailsTransfer extends AbstractTransfer {
    public DoctorInfoDetailsTransfer(String method, Client client,String messageMethod)  {
        super(method, client,messageMethod);
    }
    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String deptCode = (String) params.get("deptCode");
        String doctorCode = (String) params.get("doctorCode");
        String queryType = "3";
        Calendar calendar = Calendar.getInstance();
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("DataNode1").addElement("DataRow");
        dataRowElement.addAttribute("DEPT_CODE", deptCode).addAttribute("QUERY_TYPE", queryType).addAttribute("DOCTOR_CODE", doctorCode);
        return tplDoc.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (dataRows != null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String, Object> dataMap = new HashMap<>();
                    DoctorInfoDetailsTransfer.DoctorInfoDetailsInnerMessage dataRowMessage = new DoctorInfoDetailsTransfer.DoctorInfoDetailsInnerMessage(dataRow);
                    dataMap.put("deptCode", dataRowMessage.getDeptCode());
                    dataMap.put("docName", dataRowMessage.getDocName());
                    dataMap.put("deptName", dataRowMessage.getDeptName());
                    dataMap.put("doctorCode", dataRowMessage.getDoctorCode());
                    dataMap.put("title", dataRowMessage.getTitle());
                    dataMap.put("major", dataRowMessage.getMajor());
                    dataMap.put("introduction", dataRowMessage.getIntroduction());
                    dataList.add(dataMap);
                }
            }
            resMap.put("list", dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);


        }
        return resMap;
    }
    private class DoctorInfoDetailsInnerMessage implements InnerMessage {
        /**
         * 科室代码
         */
        private String deptCode;
        /**
         * 医生名称
         */
        private String docName;
        /**
         * 科室名称
         */
        private String deptName;
        /**
         * 医生代码
         */
        private String doctorCode;
        /**
         * 职称
         */
        private String title;
        /**
         * 擅长
         */
        private String major;
        /**
         * 简介
         */
        private String introduction;

        public DoctorInfoDetailsInnerMessage(Element element){
            this.deptCode = element.attributeValue("DEPT_CODE");
            this.docName = element.attributeValue("DOCTOR_NAME");
            this.deptName = element.attributeValue("DEPT_NAME");
            this.doctorCode = element.attributeValue("DOCTOR_CODE");
            this.title = element.attributeValue("TITLE");
            this.major = element.attributeValue("EXPERTISE");
            this.introduction = element.attributeValue("INTRODUCTION");
        }

        public String getDeptCode() { return deptCode; }

        public String getDocName() { return docName; }

        public String getDeptName() { return deptName; }

        public String getDoctorCode(){ return doctorCode; }

        public String getTitle() { return title; }

        public String getMajor() { return major; }

        public String getIntroduction() { return introduction; }

    }


}
